package TrafficControlSystem.TrafficServer;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.FlowLayout;
import javax.swing.border.LineBorder;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;

@SuppressWarnings("serial")
public class MainServerFrame extends JFrame {

	private JPanel contentPane;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {		
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {					
					MainServerFrame frame = new MainServerFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});

		//TrafficServerMain.getInstance().start();
	}

	/**
	 * Create the frame.
	 */
	public MainServerFrame() {
		setMinimumSize(new Dimension(240, 290));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 635, 693);
		setTitle("TrafficServer");
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel.getLayout();
		flowLayout.setAlignment(FlowLayout.LEFT);
		contentPane.add(panel, BorderLayout.NORTH);
		
		WindowListener windowAdapter = new WindowAdapter() {

			@Override
			public void windowClosing(WindowEvent e) {
				super.windowClosing(e);
				Logger.close();
			}
		};
		
		this.addWindowListener(windowAdapter);
		
		final JButton btnStart = new JButton("Start");
		panel.add(btnStart);
		
		SimulationView crossroadPanel = new SimulationView();
		contentPane.add(crossroadPanel, BorderLayout.CENTER);
		crossroadPanel.setBorder(new EmptyBorder(8, 8, 8, 8));
		crossroadPanel.setLayout(new BorderLayout(0, 0));
		TrafficServer.getInstance().addObserver(crossroadPanel);
		btnStart.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				TrafficServer.getInstance().start();
				btnStart.setEnabled(false);
				Logger.log("Start");
			}
		});
	}

}
